from machine import Pin, PWM

def set_pwm(pin: Pin, freq: int, duty: int) -> PWM:
    """
    Initialize PWM and set frequency and duty cycle
    """
    pwm = PWM(pin)
    pwm.freq(freq)
    pwm.duty(duty)
    return pwm
